package com.medicine.hbp.ui;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.download.NotificationUpdateActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.DownloadVo;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.MyApplication;
import com.medicine.hbp.uitl.Share;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

/**
 * 设置界面
 * @author Administrator
 *
 */
public class SettingActivity extends BaseActivity {
	@BindView(id = R.id.set_layout_01, click = true)public LinearLayout set_layout_01;//更多精彩应用
	@BindView(id = R.id.set_layout_02, click = true)public LinearLayout set_layout_02;// 分享好友
	@BindView(id = R.id.set_layout_03, click = true)public LinearLayout set_layout_03;// 应用评分
	@BindView(id = R.id.set_layout_04, click = true)public LinearLayout set_layout_04;// 建议反馈
	@BindView(id = R.id.set_layout_05, click = true)public LinearLayout set_layout_05;// 检查更新
	@BindView(id = R.id.set_layout_06, click = true)public LinearLayout set_layout_06;// 使用帮助
	@BindView(id = R.id.set_layout_07, click = true)public LinearLayout set_layout_07;// 关于应用
	private MyApplication app;
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_setting);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		app = (MyApplication) getApplication();
		initTop();
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		Intent intent = null;
		switch (v.getId()) {
			case R.id.set_layout_01://更多精彩应用
				intent=new Intent(aty,WonderfulAppActivity.class);
				break;
			case R.id.set_layout_02:// 分享好友
				Share.openShare(aty,"","测试","这只是个测试",
						"https://www.baidu.com/index.php?tn=maxdos0009_pg",
						"http://e.hiphotos.baidu.com/lvpics/h=800/sign=1d1cc1876a81800a71e5840e813533d6/5366d0160924ab185b6fd93f33fae6cd7b890bb8.jpg");
				break;
			case R.id.set_layout_03:// 应用评分
				Uri uri = Uri.parse("https://www.baidu.com/index.php?tn=maxdos0009_pg");
				Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent2);
				break;
			case R.id.set_layout_04:// 建议反馈
				intent=new Intent(aty,FeedBackActivity.class);
				break;
			case R.id.set_layout_05:// 检查更新
				http();
				break;
			case R.id.set_layout_06:// 使用帮助
				intent=new Intent(aty,HelpAndMessageActivity.class);
				intent.putExtra("type", 1);
				intent.putExtra("name", "使用帮助");
				break;
			case R.id.set_layout_07:// 关于应用
				intent=new Intent(aty,HelpAndMessageActivity.class);
				intent.putExtra("type", 2);
				intent.putExtra("name", "关于应用");
				break;
			default:
				break;
		}
		if(intent!=null){
			startActivity(intent);
		}
	}
	private void http() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(this, HttpInterface.FILE_VERSION, params,
				new AppHttpCallBack<DownloadVo>(DownloadVo.class,aty) {
					@Override
					public void onSuccess(DownloadVo result) {
						try{
							if (CommUtil.getVersionCode(aty)  < result.info.fileVersion) {
								if(CommUtil.getVersionCode(aty)  < result.info.fileMinVersion){
									fileVersion(result.info.fileUrl,result.info.fileNote);
								}else{
									fileVersion(result.info.fileUrl,result.info.fileNote);
								}

							}
						}catch(Exception e){
							ToastUtil.showShort(aty, "暂无更新");
						}

					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}
	private void fileVersion(final String url, String content) {
		AlertDialog.Builder builder = new AlertDialog.Builder(aty);
		builder.setTitle("更新");
		builder.setMessage(content);
		builder.setNegativeButton("确定", new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				app.setDownload(true);
				app.setApkUrl(url);
				Intent it = new Intent(aty,NotificationUpdateActivity.class);
				startActivity(it);

			}
		});
		builder.setNeutralButton("继续使用", null);
		builder.show();
	}
	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("设置");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}


}
